package com.sfl.api_backend.controller.quota;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfl.api_backend.constant.HttpCode;
import com.sfl.api_backend.entity.Consumption;
import com.sfl.api_backend.entity.Quota;
import com.sfl.api_backend.entity.Recharge;
import com.sfl.api_backend.service.IConsumptionService;
import com.sfl.api_backend.service.IQuotaService;
import com.sfl.api_backend.service.IRechargeService;
import com.sfl.api_backend.utils.Api.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 配额表 前端控制器
 * </p>
 *
 * @author szc
 * @since 2023-01-31
 */
@RestController
@RequestMapping("/quotas")
@Slf4j
public class QuotaController {

    @Resource
    private IQuotaService quotaService;

    @Resource
    private IConsumptionService consumptionService;

    @Resource
    private IRechargeService rechargeService;

    /**
     * 个人用户配额管理
     */
    @PostMapping("/quota/list")
    @PreAuthorize("hasAuthority('User')")
    public RestResult quotasList(@RequestBody Quota vo)
    {
        try {
            log.info("list quotas: {}", vo.getUserId());
            Wrapper<Quota> wrapper = new LambdaQueryWrapper<Quota>().eq(Quota::getUserId, vo.getUserId());
            return RestResult.success(quotaService.list(wrapper));
        }
        catch (Exception e) {
            log.error("list quotas: {} error", vo.getUserId());
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/recharge/list")
    @PreAuthorize("hasAuthority('User')")
    public RestResult rechargeList(@RequestBody Recharge vo )
    {
        try {
            log.info("list recharges: {}", vo.getUserId());
            Wrapper<Recharge> wrapper = new LambdaQueryWrapper<Recharge>().eq(Recharge::getUserId, vo.getUserId());
            return RestResult.success(rechargeService.list(wrapper));
        }
        catch (Exception e) {
            log.error("list recharges: {} error", vo.getUserId());
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/recharge/etc")
    @PreAuthorize("hasAuthority('User')")
    public RestResult rechargeEtc(@RequestBody Recharge recharge)
    {
        try {
            log.info("list recharges: {}", recharge);
            // vo -> quota vo -> recharge
            Wrapper<Quota> q_wrapper = new LambdaQueryWrapper<>();
            ((LambdaQueryWrapper<Quota>) q_wrapper).eq(Quota::getUserId, recharge.getUserId());

            Quota quota = quotaService.getOne(q_wrapper);
            quota.setQuotaNum(quota.getQuotaNum() + recharge.getRechargeNum());
            quotaService.updateById(quota);

            rechargeService.save(recharge);
            return RestResult.success();
        }
        catch (Exception e) {
            log.error("list recharges: {} error", recharge);
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/consumption/list")
    @PreAuthorize("hasAuthority('User')")
    public RestResult consumptionList(@RequestBody Consumption vo)
    {
        try {
            log.info("list consumptions: {}", vo.getUserId());
            Wrapper<Consumption> wrapper = new LambdaQueryWrapper<Consumption>().eq(Consumption::getUserId, vo.getUserId());
            return RestResult.success(consumptionService.list(wrapper));
        }
        catch (Exception e) {
            log.error("list consumptions: {} error", vo.getUserId());
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }



}
